package examples;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;



public class FFKompletni {
			
	public static void WaitForAjax(WebDriver driver) throws InterruptedException
	{
		while (true) // Handle timeout somewhere
		{
			boolean ajaxIsComplete = (boolean)((JavascriptExecutor) driver).executeScript("return Ajax.activeRequestCount == 0");
			if (ajaxIsComplete)
				break;
			Thread.sleep(100);
		}
	}
		
		public static void main(String[] args) throws InterruptedException {
	        // Create a new instance of the Firefox driver
	        // Notice that the remainder of the code relies on the interface, 
	        // not the implementation.
        for(int i=1; i<=1; i++) {
        	WebDriver driver = new FirefoxDriver();
        	driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	        
	        long start_time = MereniCpu.getUserTime();
	        
	        driver.get("http://localhost:8080/tryouts/loginpage");
	                
	        // Prihlaseni
	        WebElement element = driver.findElement(By.name("userName"));
	        element.sendKeys("test");        
	        WebElement element1 = driver.findElement(By.name("password"));
	        element1.sendKeys("test");
	        element.submit();
	        
	        // Page Address
	        // Vybereme company
	        element = driver.findElement(By.id("tCompany"));
	        element.click();
	        // Vybere person
	        element = driver.findElement(By.id("tPerson"));
	        element.click();
	        
	        //vyplneni v poradku
	        List<WebElement> elements = driver.findElements(By.className("userDataInput"));
	        elements.get(0).sendKeys("Jan");
	        elements.get(1).sendKeys("Novák");
	        elements.get(2).sendKeys("Testovací");
	        elements.get(3).sendKeys("1100");
	        elements.get(3).sendKeys("0");
	        
	        WaitForAjax(driver);
	        WebElement myDynamicElement = (new WebDriverWait(driver, 10))
	      		  .until(new ExpectedCondition<WebElement>(){
	      			@Override
	      			public WebElement apply(WebDriver d) {
	      				return d.findElement(By.className("inZone1"));
	      			}});
	        
	        elements.get(6).sendKeys("Jan");
	        elements.get(7).sendKeys("Novák");
	        elements.get(8).sendKeys("Přechodná");
	        elements.get(9).sendKeys("1100");
	        elements.get(9).sendKeys("0");
	        
	        WaitForAjax(driver);
	        
	        myDynamicElement = (new WebDriverWait(driver, 10))
	        		  .until(new ExpectedCondition<WebElement>(){
	        			@Override
	        			public WebElement apply(WebDriver d) {
	        				return d.findElement(By.className("inZone1"));
	        			}});
	        myDynamicElement.submit();
	
	        //pocitani iteraci
	        if(driver.getTitle().equals("Summary"))	{
	        	System.out.println("Pocet probehlych testu:" + i + " test probehl OK");
	        	long end_time = MereniCpu.getUserTime();
	        	System.out.println("Delka trvani:" + (end_time-start_time));
	        	MereniCpu.CPUprehled();
	        }
	        else
	        {
	        	System.out.println("Pocet probehlych testu:" + i + " test neprobehl v poradku");
	        }
	        
	        driver.quit();
	        
	        driver = new FirefoxDriver();
	        
	        
	        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	        start_time = MereniCpu.getUserTime();
	        driver.get("http://localhost:8080/tryouts/loginpage");

	        //System.out.println(js.executeScript("return document.title"));
	        
	        
	        // Prihlaseni
	        element = driver.findElement(By.name("userName"));
	        element.sendKeys("test");        
	        element1 = driver.findElement(By.name("password"));
	        element1.sendKeys("test");
	        element.submit();
	        
	        // Page Address
	        // Vybereme company
	        myDynamicElement = (new WebDriverWait(driver, 10))
	      		  .until(new ExpectedCondition<WebElement>(){
	      			@Override
	      			public WebElement apply(WebDriver d) {
	      				return d.findElement(By.id("tCompany"));
	      			}});
	        myDynamicElement.click();
	        
	        //vyplneni v poradku
	        elements = driver.findElements(By.className("userDataInput"));
	        elements.get(0).sendKeys("Softec");
	        elements.get(1).sendKeys("Empiria");
	        elements.get(2).sendKeys("Pankrac");
	        elements.get(3).sendKeys("1100");
	        elements.get(3).sendKeys("0");
	        WaitForAjax(driver);

	        
	        myDynamicElement = (new WebDriverWait(driver, 10))
	    			.until(new ExpectedCondition<WebElement>(){
	    			@Override
	    			public WebElement apply(WebDriver d) {
	    				return d.findElement(By.className("inZone1"));
	    			}});
	        
	        myDynamicElement.submit();
	        
	                
       
	        //Company profile
	        Select select = new Select(driver.findElement(By.id("okecSelected")));
	        select.selectByVisibleText("Rybolov a chov ryb");
	        
	        WaitForAjax(driver);
	        myDynamicElement = (new WebDriverWait(driver, 10))
	      		  .until(new ExpectedCondition<WebElement>(){
	      			@Override
	      			public WebElement apply(WebDriver d) {
	      				return d.findElement(By.className("textFieldInZone"));
	      			}});
	        myDynamicElement.sendKeys("724222333");        
	        
	        
	        myDynamicElement = (new WebDriverWait(driver, 10))
	        		  .until(new ExpectedCondition<WebElement>(){
	        			@Override
	        			public WebElement apply(WebDriver d) {
	        				return d.findElement(By.tagName("textarea"));
	        			}});
	       
	        myDynamicElement.sendKeys("Nejaky popis.");    
	        myDynamicElement.submit();
	        
	        
	        if(driver.getTitle().equals("Summary"))	{
	        	System.out.println("Pocet probehlych testu:" + i + " test probehl OK");
	        	long end_time = MereniCpu.getUserTime();
	        	System.out.println("Delka trvani:" + (end_time-start_time));
	        	MereniCpu.CPUprehled();
	        }
	        else
	        {
	        	System.out.println("Pocet probehlych testu:" + i + " test neprobehl v poradku");
	        }
	        //driver.quit();
	    }
	}
}
